package com.tenglong.teacher.ui.adapter.viewholder.homework;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.cuotibao.zhuoxiao.basics.GlobalConstant;
import com.cuotibao.zhuoxiao.basics.utils.IpUtils;
import com.cuotibao.zhuoxiao.basics.utils.glide.GlideOptions;
import com.tenglong.teacher.R;
import com.tenglong.teacher.data.resultInfo.homework.HomeWork;
import com.tenglong.teacher.data.resultInfo.homework.HomeWorkStateItem;
import com.tenglong.teacher.services.hdx.CreateRequestEntity;
import com.tenglong.teacher.ui.adapter.HomeworkStudentListAdapter;
import com.tenglong.teacher.ui.adapter.viewholder.base.BaseViewHolder;
import com.tenglong.teacher.ui.homework.HomeworkDetailListActivity;
import com.tenglong.teacher.ui.utils.MyUtils;

import java.util.Date;

/**
 * @author hdx on 2019/3/13.
 */
public class StudentCommitViewHolder extends BaseViewHolder<HomeWork> implements View.OnClickListener {

    private TextView tv_studentName;
    private ImageView iv_studentAvatar;
    private TextView tv_className;
    private TextView tv_createDate;
    private TextView tv_createTime;
    private HomeWorkStateItem data;
    private Context mContext;
    private ImageView itemState;
    private HomeworkStudentListAdapter.HomeworkStudentListListener listListener;

    public StudentCommitViewHolder(View itemView) {
        super(itemView);
        this.mContext = itemView.getContext();
        initView();
        setListeners();
    }

    public StudentCommitViewHolder(View itemView, HomeworkStudentListAdapter.HomeworkStudentListListener listListener) {
        this(itemView);
        this.listListener = listListener;
    }

    private void setListeners() {
        itemView.setTag("onClick");
        itemView.setOnClickListener(this);
    }

    private void initView() {
        iv_studentAvatar = itemView.findViewById(R.id.iv_header);
        tv_createDate = itemView.findViewById(R.id.tv_year_month);
        tv_studentName = itemView.findViewById(R.id.tv_stu_name);
        tv_className = itemView.findViewById(R.id.tv_class_name);
        tv_createTime = itemView.findViewById(R.id.tv_task_time);
        itemState = itemView.findViewById(R.id.itemState);
    }

    @Override
    public void setLayoutInfo(HomeWork info) {
        this.data = (HomeWorkStateItem) info;
        Date lastUpdateDate = data.getLastUpdateDate();
        if (lastUpdateDate != null) {
            tv_createDate.setText(MyUtils.getDateTimeByFormat(lastUpdateDate, "yyyy-MM-dd"));
            tv_createTime.setText(MyUtils.getDateTimeByFormat(lastUpdateDate, "HH:mm:ss"));
        }
        tv_studentName.setText(TextUtils.isEmpty(data.getStudentName()) ? "" : data.getStudentName());
        tv_className.setText(TextUtils.isEmpty(data.getClassName()) ? "" : data.getClassName());
        RequestOptions options = GlideOptions.getGlideRequestOptions().
                error(R.drawable.setting_image_defauft_header).
                placeholder(R.drawable.header_image_loading);
        Glide.with(mContext)
                .load(IpUtils.getImageUrl("/uploaded/" + data.getStudentAvatar()))
                .apply(options)
                .into(iv_studentAvatar);
        itemState.setBackgroundResource(itemStateImage());
    }

    /**
     * 0、已发布（学生端专有）
     * 1、未批改（原已提交）
     * 2、待确认
     * 3、已完成
     * 4、待改错
     * 5、已改错
     * 6、已截止
     *
     * @return
     */
    private int itemStateImage() {
        switch (data.getStatus()) {
            case 0:
                return R.drawable.yifabu;
            case 1:
                return R.drawable.weipigai;
            case 2:
                return R.drawable.daiqueren;
            case 3:
                return R.drawable.yiwancheng;
            case 4:
                return R.drawable.qingxiugai;
            case 5:
                return R.drawable.yigaicuo;
            case 6:
                return R.drawable.yijiezhi;
            default:
                return 0;
        }
    }

    @Override
    public void onClick(View v) {
        if ("onClick".equals(v.getTag())) {
            Intent intent = new Intent();
            intent.setClass(mContext, HomeworkDetailListActivity.class);
//            Intent intent = new Intent("com.tenglong.teacher.HomeWorkDetailActivity");
            intent.putExtra("sessionid", TextUtils.isEmpty(data.getSessionId()) ? "" : data.getSessionId());
            String studentName = data.getStudentName();
            if (!TextUtils.isEmpty(studentName)) {
                intent.putExtra("title", studentName);
            }
            intent.putExtra("accid", TextUtils.isEmpty(data.getStudentAccId()) ? "" : data.getStudentAccId());
            ((Activity) mContext).startActivityForResult(intent, GlobalConstant.REQUEST_CODE_HOMEWORK_DETAIL_ACTIVITY);
            if (listListener != null) {
                listListener.onRecordPosition(getAdapterPosition());
            }
        }
    }
}
